package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.common.constant.Constants;
import cn.itsource.ymcc.common.constant.OrderStatus;
import cn.itsource.ymcc.common.constant.RocketConstants;
import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.service.ICourseOrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Date;

@Component
@RocketMQMessageListener(consumerGroup = "order_status_consumer",
        //Topic
        topic = RocketConstants.UPDATE_ORDER_STATUS_TOPIC,
        //TAGS
        selectorExpression = RocketConstants.UPDATE_ORDER_STATUS_TAG
)
public class OrderStatusConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private ICourseOrderService courseOrderService;
    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String orderNo = new String(body, Constants.UTF8);
            CourseOrder one = courseOrderService.getOne(new LambdaQueryWrapper<CourseOrder>().eq(CourseOrder::getOrderNo, orderNo));
            one.setUpdateTime(new Date());
            one.setStatusOrder(OrderStatus.ORDER_PAY);
            courseOrderService.updateById(one);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
